﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;
using Vladsm.Web.UI.WebControls;

namespace AB.ServiceDirectory.Design.AContact
{
    public partial class ContactList01 : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var filter = new AContactFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            GroupRadioButton contact = null;
            if (ContactGridView.Rows.Count > 0)
            {
                contact = ContactGridView.Rows[0].FindControl("RadioChooseContact") as GroupRadioButton;
            }

            if (contact != null)
            {
                contact.Checked = true;
            }
            base.OnPreRender(e);
        }

        //public void Popup()
        //{
        //   // PanelContainer.CssClass = "popup_container";
        //    //PanelContainer.Visible = true;
        //}

        /// <summary>
        /// Handles the Click event of the ButtonSearchContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSearchContact_Click(object sender, EventArgs e)
        {
            string firstName = TextBoxFirstNameSearch.Text;
            string Surname = TextBoxSurnameSearch.Text;

            var filter = ViewState["filter"] as AContactFilter;
            if (filter == null)
            {
                return;
            }

            filter.Surname = Surname.Trim();
            filter.FirstName = firstName.Trim();

            ContactGridView.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the ButtonClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClose_Click(object sender, EventArgs e)
        {
            //PanelContainer.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the ButtonSelect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSelect_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow contactRow in ContactGridView.Rows)
            {
                if (contactRow.RowType == DataControlRowType.DataRow)
                {
                    var grd = (GroupRadioButton) contactRow.FindControl("RadioChooseContact");
                    if (grd != null && grd.Checked)
                    {
                        DataKey dataKey = ContactGridView.DataKeys[contactRow.RowIndex];
                        if (dataKey != null)
                        {
                            if (contactRow.RowIndex < 0)
                            {
                                return;
                            }


                            var key = (int) dataKey.Value;
                            Contact contact = AContactBO.FindContactById(key);


                            Control textBoxContactName = Parent.FindControl("TextBoxManagerName");
                            Control hiddenFieldManagerId = Parent.FindControl("HiddenFieldManagerID");

                            if (textBoxContactName != null)
                            {
                                (textBoxContactName as TextBox).Text = contact.FirstName + " " + contact.Surname;
                            }

                            if (hiddenFieldManagerId != null)
                            {
                                (hiddenFieldManagerId as HiddenField).Value = key.ToString(CultureInfo.CurrentCulture);
                            }
                        }
                    }
                }
            }
            // PanelContainer.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the ButtonCreate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCreate_Click(object sender, EventArgs e)
        {
            // PanelContainer.Visible = false;
            Response.Redirect("AddContact.aspx");
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ContactList.aspx");
        }

        /// <summary>
        /// Handles the Selecting event of the ContactObjectDataSource control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs"/> instance containing the event data.</param>
        protected void ContactObjectDataSource_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as AContactFilter;
            if (filter != null)
            {
                e.InputParameters["filter"] = filter;
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonNone control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonNone_Click(object sender, EventArgs e)
        {
            //var textBoxContactName = Parent.FindControl("TextBoxManagerName");
            //var hiddenFieldManagerID = Parent.FindControl("HiddenFieldManagerID");

            //if (textBoxContactName != null)
            //{
            //    (textBoxContactName as TextBox).Text = string.Empty;
            //}

            //if (hiddenFieldManagerID != null)
            //{
            //    (hiddenFieldManagerID as HiddenField).Value = string.Empty;
            //}

            // pa.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the ButtonClear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClear_Click(object sender, EventArgs e)
        {
            TextBoxFirstNameSearch.Text = string.Empty;
            TextBoxSurnameSearch.Text = string.Empty;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as AContactFilter;
            if (filter != null)
            {
                filter.IncludeInactive = CheckBoxInactive.Checked;
            }

            ContactGridView.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the ButtonEdit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow contactRow in ContactGridView.Rows)
            {
                if (contactRow.RowType == DataControlRowType.DataRow)
                {
                    var grd = (GroupRadioButton) contactRow.FindControl("RadioChooseContact");
                    if (grd != null && grd.Checked)
                    {
                        DataKey dataKey = ContactGridView.DataKeys[contactRow.RowIndex];
                        if (dataKey != null)
                        {
                            if (contactRow.RowIndex < 0)
                            {
                                return;
                            }

                            var key = (int) dataKey.Value;
                            Response.Redirect("EditContact.aspx?contactId=" + key);
                        }
                    }
                }
            }
        }
    }
}